/**
 * @UiMenubar.cpp
 * Copyright 2013 (C) FatalEror / Dean Bahari
 * 
 * UiPack is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * See LICENSE file in main folder for more information.
 */

#include "UiCommon.hpp"
#include "UiApp.hpp"
#include "UiBase.hpp"
#include "UiButton.hpp"
#include "UiMenubar.hpp"
#include "UiResource.hpp"
#include "UiWindow.hpp"

UiMenubarItem::UiMenubarItem(const wchar_t *name, const wchar_t *imageDir, void *callback, void *param)
	:UiButton(name)
{
	this->setImage(imageDir);
	this->setCallback(callback, param);
}

void UiMenubarItem::onPaint(UiEvent &ev)
{
	Gdiplus::Graphics grBuffer(ev.getDC());
	Gdiplus::Image *img = this->getImage();
	grBuffer.FillRectangle(UiApp::getResource()->getBrush(UiResource::UI_BRUSH_BORDER), 0, 0, 28, 28);
	if (this->getState() & UiControl::UI_SELECTED) {
		grBuffer.DrawImage(UiApp::getResource()->getImage(UiResource::UI_IMAGE_MENUBARITEM_CLICK), Gdiplus::Rect(1, 0, 27, 28), 0, 0, 26, 28, Gdiplus::UnitPixel);
		grBuffer.DrawImage(img, Gdiplus::Rect(1, 2, 26, 26), 0, 0, img->GetWidth(), img->GetHeight(), Gdiplus::UnitPixel);
	} else {
		if (this->getState() & UiControl::UI_HOVERED)
			grBuffer.DrawImage(UiApp::getResource()->getImage(UiResource::UI_IMAGE_MENUBARITEM_HOVER), Gdiplus::Rect(1, 0, 27, 28), 0, 0, 26, 28, Gdiplus::UnitPixel);
		grBuffer.DrawImage(img, Gdiplus::Rect(1, 1, 26, 26), 0, 0, img->GetWidth(), img->GetHeight(), Gdiplus::UnitPixel);
	}
}

UiMenubar::UiMenubar()
{
}

UiMenubar::UiMenubar(UiWindow *parent)
{
	parent->setMenubar(this);
}

void UiMenubar::addItem(UiMenubarItem *item)
{
	this->addChild(item);
	ShowWindow(item->getHandle(), SW_SHOWDEFAULT);
	item->setPosition(8 - UI_WINDOWSIZE_BORDER + 32 * (this->getChildCount() - 1), 0);
	item->setSize(28, 28);
}

LRESULT CALLBACK UiMenubar::handleCustomProc(UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch (msg) {
	case WM_ERASEBKGND:
		return 0;
	case WM_PAINT:
	{
		PAINTSTRUCT ps;
		HDC hDC = BeginPaint(this->getHandle(), &ps);
		HDC hDCBuffer = UiApp::getCurrentApp()->getDCBuffer();

		Gdiplus::Graphics grBuffer(hDCBuffer);
		grBuffer.FillRectangle(UiApp::getResource()->getBrush(UiResource::UI_BRUSH_BORDER), 0, 0, this->getWidth(), this->getHeight());
		grBuffer.DrawLine(UiApp::getResource()->getPen(UiResource::UI_PEN_BORDER), 0, 31, this->getWidth(), 31);

		BitBlt(hDC, 0, 0, this->getWidth(), this->getHeight(), hDCBuffer, 0, 0, SRCCOPY);

		EndPaint(this->getHandle(), &ps);
	} break;
	default:
		return DefWindowProc(this->getHandle(), msg, wParam, lParam);
	}
	return 0;
}